package cmd

import (
	"context"
	"fmt"
	"github.com/gogf/gf/v2/encoding/gjson"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/os/gcmd"
	"os"
	"wh-1-backend/internal/dao"
	"wh-1-backend/internal/model"
	"wh-1-backend/internal/model/do"
	"wh-1-backend/internal/service"

	"wh-1-backend/internal/controller"
)

var (
	Main = gcmd.Command{
		Name:  "main",
		Usage: "main",
		Brief: "start http server",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			s := g.Server()
			s.Group("/", func(group *ghttp.RouterGroup) {
				group.Middleware(ghttp.MiddlewareHandlerResponse)
				group.Bind(
					controller.Hello,
				)
			})
			s.Group("/api/v1", func(group *ghttp.RouterGroup) {
				group.Middleware(service.Middleware().MiddlewareHandlerResponse)
				group.Bind(controller.Auth.Login)
				group.Group("/", func(group *ghttp.RouterGroup) {
					group.Middleware(service.Middleware().Auth)
					group.Bind(controller.Auth.Logout)
					group.Bind(controller.Auth.UserProfile)
					group.Bind(controller.Menu)
					group.Bind(controller.Role)
					group.Bind(controller.User)
					group.Bind(controller.City)
					group.Bind(controller.Industry)
					group.Bind(controller.Function)
					group.Bind(controller.Dictionary)
					group.Bind(controller.Company)
					group.Bind(controller.Resume)
					group.Bind(controller.Project)
				})
			})
			s.Run()
			return nil
		},
	}
	Test = &gcmd.Command{
		Name:  "test",
		Usage: "main test",
		Brief: "test -- 测试 ！！",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			fmt.Println("测试")
			//err = test(ctx)
			err = test1(ctx)
			return
		},
	}
)

type DictItem struct {
	Code  string `json:"code"`
	Value string `json:"value"`
	Type  string `json:"type"`
	Color string `json:"color"`
}

func test1(ctx context.Context) (err error) {
	data, err := os.ReadFile("test.json")
	if err != nil {
		return
	}
	j, err := gjson.DecodeToJson(data)
	if err != nil {
		return
	}
	var list []*DictItem
	err = j.Scan(&list)
	if err != nil {
		return
	}
	err = insertDictItem(ctx, list)
	return
}

func insertDictItem(ctx context.Context, list []*DictItem) (err error) {
	for _, item := range list {
		_, err = dao.DictionaryItem.Ctx(ctx).Insert(do.DictionaryItem{
			Code:  item.Code,
			Value: item.Value,
			Type:  item.Type,
			Color: item.Color,
		})
		if err != nil {
			return
		}
	}
	return
}

func test(ctx context.Context) (err error) {
	data, err := os.ReadFile("test.json")
	if err != nil {
		return
	}
	j, err := gjson.DecodeToJson(data)
	if err != nil {
		return
	}
	//var list []*model.CitySiTree
	//var list []*model.IndustryTree
	var list []*FunctionTree
	err = j.Scan(&list)
	if err != nil {
		return
	}
	//err = insertCity(ctx, list)
	//err = insertIndustry(ctx, list)
	err = insertFunction(ctx, list)
	fmt.Println("City 导入完成！！")
	return
}

func insertCity(ctx context.Context, tree []*model.CitySiTree) (err error) {
	for _, leaf := range tree {
		_, err = dao.City.Ctx(ctx).InsertIgnore(do.City{
			Id:    leaf.Id,
			Pid:   leaf.ParentId,
			Label: leaf.Label,
		})
		if err != nil {
			return
		}
		if len(leaf.Children) > 0 {
			err = insertCity(ctx, leaf.Children)
			if err != nil {
				return err
			}
		}
	}
	return nil
}

func insertIndustry(ctx context.Context, tree []*model.IndustryTree) (err error) {
	for _, leaf := range tree {
		_, err = dao.Industry.Ctx(ctx).InsertIgnore(do.Industry{
			Id:    leaf.Id,
			Label: leaf.Label,
		})
		if err != nil {
			return
		}
		if len(leaf.Children) > 0 {
			err = insertIndustry(ctx, leaf.Children)
			if err != nil {
				return err
			}
		}
	}
	return nil
}

type FunctionTree struct {
	Id       uint            `json:"id"        ` //
	ParentId uint            `json:"parent_id"`
	Label    string          `json:"label"     ` //
	Children []*FunctionTree `json:"children"`
}

func insertFunction(ctx context.Context, tree []*FunctionTree) (err error) {
	for _, leaf := range tree {
		_, err = dao.Function.Ctx(ctx).InsertIgnore(do.Function{
			Id:    leaf.Id,
			Pid:   leaf.ParentId,
			Label: leaf.Label,
		})
		if err != nil {
			return
		}
		if len(leaf.Children) > 0 {
			err = insertFunction(ctx, leaf.Children)
			if err != nil {
				return err
			}
		}
	}
	return nil
}
